package com.yc.sole.erp.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.erp.dto.ShipmentOrderDto;
import com.yc.sole.erp.entity.ShipmentOrder;
import com.yc.sole.erp.mapper.ShipmentOrderMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2024-03-11
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class ShipmentOrderRepository extends ServiceImpl<ShipmentOrderMapper, ShipmentOrder> {

    /**
     * 新增
     *
     * @param shipmentOrderDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveShipmentOrder(ShipmentOrderDto shipmentOrderDto) {
        ShipmentOrder entity = new ShipmentOrder();
        BeanUtil.copyProperties(shipmentOrderDto, entity);
        this.save(entity);
    }

    /**
     * 新增
     *
     * @param shipmentOrderDtoList
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveShipmentOrder(Collection<ShipmentOrderDto> shipmentOrderDtoList) {
        List<ShipmentOrder> shipmentOrderList = BeanUtil.copyToList(shipmentOrderDtoList, ShipmentOrder.class);
        this.saveBatch(shipmentOrderList);
    }

    /**
     * 通过Id修改
     *
     * @param shipmentOrderDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateShipmentOrderById(ShipmentOrderDto shipmentOrderDto) {
        ShipmentOrder entity = new ShipmentOrder();
        BeanUtil.copyProperties(shipmentOrderDto, entity);
        this.updateById(entity);
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteShipmentOrderByIds(Collection<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 通过送货单id批量删除
     *
     * @param shipmentId
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteByShipmentId(Long shipmentId) {
        LambdaQueryWrapper<ShipmentOrder> wrapper = Wrappers.<ShipmentOrder>lambdaQuery().eq(ShipmentOrder::getShipmentId, shipmentId);
        this.remove(wrapper);
    }

    /**
     * 根据id获取
     *
     * @param ids
     * @return RestResult
     */
    public List<ShipmentOrderDto> selectShipmentOrderByIds(Set<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(this.listByIds(ids), ShipmentOrderDto.class);
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return RestResult
     */
    public ShipmentOrderDto selectShipmentOrderById(Long id) {
        return BeanUtil.copyProperties(this.getById(id), ShipmentOrderDto.class);
    }

    /**
     * 根据送货单id获取
     *
     * @param id
     * @return RestResult
     */
    public List<ShipmentOrderDto> selectByShipmentId(Long id) {
        LambdaQueryWrapper<ShipmentOrder> wrapper = Wrappers.<ShipmentOrder>lambdaQuery().eq(ShipmentOrder::getShipmentId, id);
        List<ShipmentOrder> shipmentOrderList = this.list(wrapper);
        return BeanUtil.copyToList(shipmentOrderList, ShipmentOrderDto.class);
    }

    /**
     * 根据送货单id获取
     *
     * @param ids
     * @return RestResult
     */
    public List<ShipmentOrderDto> selectByShipmentIds(Set<Long> ids) {
        LambdaQueryWrapper<ShipmentOrder> wrapper = Wrappers.<ShipmentOrder>lambdaQuery().in(ShipmentOrder::getShipmentId, ids);
        List<ShipmentOrder> shipmentOrderList = this.list(wrapper);
        return BeanUtil.copyToList(shipmentOrderList, ShipmentOrderDto.class);
    }

    /**
     * 根据送货单id查询关联订单id
     *
     * @param shipmentId
     * @return
     */
    public Set<Long> selectOrderIdByShipmentId(Long shipmentId) {
        LambdaQueryWrapper<ShipmentOrder> wrapper = Wrappers.<ShipmentOrder>lambdaQuery()
                .select(ShipmentOrder::getOrderId)
                .eq(ShipmentOrder::getShipmentId, shipmentId);
        return this.list(wrapper).stream().map(ShipmentOrder::getOrderId).collect(Collectors.toSet());
    }

    /**
     * 根据订单号查询
     *
     * @param orderId
     * @return
     */
    public List<ShipmentOrderDto> selectByOrderId(Long orderId) {
        LambdaQueryWrapper<ShipmentOrder> wrapper = Wrappers.<ShipmentOrder>lambdaQuery().eq(ShipmentOrder::getOrderId, orderId);
        return BeanUtil.copyToList(this.list(wrapper), ShipmentOrderDto.class);
    }
}
